﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UniversalDataAccess.BusinessEntities;

namespace UniversalDataAccess.DataAccess
{
    public class StreamDataAccess : EducationalDivisionsDataAccess
    {
        public StreamDataAccess(String dbName, String conn, Boolean usePredefinedMetadata)
            : base(dbName, conn, usePredefinedMetadata)
        {
        }

        public List<Stream> SelectStreamsList()
        {
            List<EducationalDivision> divList = SelectEducationalDivisionsList();

            List<STREAM> strList = dataContext.STREAM.ToList();

            //Merge result
            var res = (from div in divList
                       join str in strList on div.ID equals str.ID
                       select new Stream
                       {
                           DataBaseName = dataBaseName,
                           CLASS_ID = div.CLASS_ID,
                           EXT_ID = div.EXT_ID,
                           ID = div.ID,
                           NAME = div.NAME,
                           PARENT_ID = div.PARENT_ID,
                           BOSS_ID = div.BOSS_ID,
                           DATE_CREATED = div.DATE_CREATED,
                           DESCRIPTION = div.DESCRIPTION,
                           LECTIONS = (Double)str.LECTIONS,
                           PRACTIC = (Double)str.PRACTIC
                       }).ToList();

            return res;
        }

        public Stream SelectStreamBy_Id(int Id)
        {
            try
            {
                EducationalDivision basePart = SelectEducationalDivisionBy_Id(Id);

                List<STREAM> strPart = dataContext.STREAM.ToList();

                //Merge result
                var res = (from str in strPart
                           where str.ID == basePart.ID
                           select new Stream
                           {
                               DataBaseName = dataBaseName,
                               CLASS_ID = basePart.CLASS_ID,
                               EXT_ID = basePart.EXT_ID,
                               ID = basePart.ID,
                               NAME = basePart.NAME,
                               PARENT_ID = basePart.PARENT_ID,
                               BOSS_ID = basePart.BOSS_ID,
                               DATE_CREATED = basePart.DATE_CREATED,
                               DESCRIPTION = basePart.DESCRIPTION,
                               LECTIONS = (Double)str.LECTIONS,
                               PRACTIC = (Double)str.PRACTIC
                           }).FirstOrDefault();

                return res;
            }
            catch
            {
                throw new Exception("Error while SelectStreamById");
            }
        }

        public void InsertStream(Stream str, ref int generated_Id)
        {
            try
            {
                InsertEducationalDivision(str, ref generated_Id);

                int new_Id = generated_Id;
                STREAM strPart = new STREAM();
                strPart.DIVISION = dataContext.DIVISION.Where(t => t.ID == new_Id).FirstOrDefault();
                strPart.ID = new_Id;
                strPart.LECTIONS = str.LECTIONS;
                strPart.PRACTIC = str.PRACTIC;

                //Submit changes
                dataContext.AddToSTREAM(strPart);
                dataContext.SaveChanges();
            }
            catch (Exception ex)
            {
                throw new Exception("Error while InsertStream");
            }
        }

        public void UpdateStream(Stream str)
        {
            try
            {
                UpdateEducationalDivision(str);

                STREAM strPart = dataContext.STREAM.Where(t => t.ID == str.ID).FirstOrDefault();

                strPart.LECTIONS = str.LECTIONS;
                strPart.PRACTIC = str.PRACTIC;

                //Submit changes
                dataContext.SaveChanges();
            }
            catch (Exception ex)
            {
                throw new Exception("Error while UpdateStream");
            }
        }

        public void DeleteStream(Stream str)
        {
            try
            {
                STREAM strPart = dataContext.STREAM.Where(t => t.ID == str.ID).FirstOrDefault();

                dataContext.DeleteObject(strPart);
                dataContext.SaveChanges();

                DeleteEducationalDivision(str);
            }
            catch (Exception ex)
            {
                throw new Exception("Error while DeleteStream");
            }
        }
    }
}
